package pattern;

import lombok.extern.slf4j.Slf4j;

/**
 * interrupt 两阶段中止
 */
public class TPLTerminate {
    public static void main(String[] args) throws InterruptedException {
        TPLInterrupt tpl = new TPLInterrupt();
        tpl.start();
        Thread.sleep(3000);
        tpl.stop();
    }
}

@Slf4j
class TPLInterrupt{
    Thread monitor;

    public void start() {
        monitor = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    log.debug("被打断...");
                    break;
                }
                log.debug("监控....");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    Thread.currentThread().interrupt();
                }
            }
            log.debug("料理后事...");
        });
        monitor.start();
    }

    public void stop() {
        monitor.interrupt();
    }
}
